﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Web.CommandUI;
using System.Web.UI.WebControls;

namespace PeppeDotNet.Html5VideoWebPart.Html5VideoWebPart
{
    [ToolboxItemAttribute(false)]
    public class Html5VideoWebPart : System.Web.UI.WebControls.WebParts.WebPart, IWebPartPageComponentProvider
    {
        #region fields
        private string _videoUrl;
        #endregion

        #region Ribbon xml declarations
        private string contextualTab = @"            
               <ContextualGroup Color=""Orange"" Command=""Html5VideoTab.EnableContextualGroup"" Id=""Ribbon.Html5VideoTabGroup"" Title=""HTML5"" Sequence=""502"" ContextualGroupId=""Html5VideoTabGroup"">
                  <Tab Id=""Ribbon.Html5VideoRibbon"" Title=""Video player"" Description=""Control the video from the ribbon toolbar"" Command=""Html5VideoTab.EnableCustomTab"" Sequence=""501"">
                    <Scaling Id=""Ribbon.Html5VideoRibbon.Scaling"">
                      <MaxSize Id=""Ribbon.Html5VideoRibbon.MaxSize"" GroupId=""Ribbon.Html5VideoRibbon.Player"" Size=""OneLargeTwoMedium""/>
                      <Scale Id=""Ribbon.Html5VideoRibbon.Scaling.CustomTabScaling"" GroupId=""Ribbon.Html5VideoRibbon.Player"" Size=""OneLargeTwoMedium"" />
                    </Scaling>
                    <Groups Id=""Ribbon.Html5VideoRibbon.Groups"">
                      <Group Id=""Ribbon.Html5VideoRibbon.Player"" Description=""Video player"" Title=""Video actions"" Command=""Html5VideoTab.EnableCustomGroup"" Sequence=""52"" Template=""Ribbon.Templates.Html5VideoTemplate"">
                        <Controls Id=""Ribbon.Html5VideoRibbon.Player.Controls"">
                          <Button Id=""Ribbon.Html5VideoRibbon.Player.Play"" Image32by32=""/_layouts/images/PeppeDotNet.Html5VideoWebPart/play.png"" Command=""Html5VideoTab.Play"" Sequence=""10"" Description=""Play the video"" LabelText=""Play"" TemplateAlias=""custl1""/>
                          <Button Id=""Ribbon.Html5VideoRibbon.Player.Pause"" Image32by32=""/_layouts/images/PeppeDotNet.Html5VideoWebPart/pause.png"" Command=""Html5VideoTab.Pause"" Sequence=""20"" Description=""Pause the video"" LabelText=""Pause"" TemplateAlias=""custl2""/>
                          <Button Id=""Ribbon.Html5VideoRibbon.Player.Stop"" Image32by32=""/_layouts/images/PeppeDotNet.Html5VideoWebPart/stop.png"" Command=""Html5VideoTab.Stop"" Sequence=""30"" Description=""Stop the video"" LabelText=""Stop"" TemplateAlias=""custl3""/>
                          <Button Id=""Ribbon.Html5VideoRibbon.Player.VolumeUp"" Image16by16=""/_layouts/images/PeppeDotNet.Html5VideoWebPart/plus.png"" Command=""Html5VideoTab.VolumeUp"" Sequence=""10"" Description=""Volume up"" LabelText=""Increase volume"" TemplateAlias=""custr1""/>
                          <Button Id=""Ribbon.Html5VideoRibbon.Player.VolumeDown"" Image16by16=""/_layouts/images/PeppeDotNet.Html5VideoWebPart/minus.png"" Command=""Html5VideoTab.VolumeDown"" Sequence=""20"" Description=""Volume down"" LabelText=""Decrease volume"" TemplateAlias=""custr2""/>
                          <CheckBox Id=""Ribbon.Html5VideoRibbon.Player.Mute"" Command=""Html5VideoTab.Mute"" Sequence=""30"" Description=""Mute vide"" LabelText=""Mute"" TemplateAlias=""custr3"" />
                        </Controls>
                      </Group>
                    </Groups>
                  </Tab>
               </ContextualGroup>";

        private string contextualTabTemplate = @"
          <GroupTemplate Id=""Ribbon.Templates.Html5VideoTemplate"">
            <Layout Title=""OneLargeTwoMedium"" LayoutTitle=""OneLargeTwoMedium"">
              <Section Alignment=""Top"" Type=""OneRow"">
                <Row>
                  <ControlRef DisplayMode=""Large"" TemplateAlias=""custl1"" />
                  <ControlRef DisplayMode=""Large"" TemplateAlias=""custl2"" />
                  <ControlRef DisplayMode=""Large"" TemplateAlias=""custl3"" />
                </Row>
              </Section>
              <Section Alignment=""Top"" Type=""ThreeRow"">
                <Row>
                  <ControlRef DisplayMode=""Medium"" TemplateAlias=""custr1"" />
                </Row>
                <Row>
                  <ControlRef DisplayMode=""Medium"" TemplateAlias=""custr2"" />
                </Row>
                <Row>
                  <ControlRef DisplayMode=""Medium"" TemplateAlias=""custr3"" />
                </Row>
              </Section>
            </Layout>
          </GroupTemplate>"; 
        #endregion

        #region Properties
        public string DelayScript
        {
            get
            {
                string webPartPageComponentId = SPRibbon.GetWebPartPageComponentId(this);
                return @"<script type=""text/javascript"">
                            //<![CDATA[ 
                            function AddHtml5VideoComponent()
                            {
                                var html5VideoComponent = new Html5VideoTabWebPart.CustomPageComponent('" + webPartPageComponentId + @"');
                                SP.Ribbon.PageManager.get_instance().addPageComponent(html5VideoComponent);
                            } 
                            function _registerCustomPageComponent()
                            {
                                SP.SOD.registerSod(""Html5VideoTabPageComponent.js"", ""\/_layouts\/PeppeDotNet.Html5VideoWebPart\/Html5VideoTabPageComponent.js"");
                                SP.SOD.executeFunc(""Html5VideoTabPageComponent.js"", ""Html5VideoTabWebPart.CustomPageComponent"", AddHtml5VideoComponent);
                            }
                            SP.SOD.executeOrDelayUntilScriptLoaded(_registerCustomPageComponent, ""sp.ribbon.js"");
                            //]]>
                        </script>";
            }
        }

        public WebPartContextualInfo WebPartContextualInfo
        {
            get
            {
                WebPartContextualInfo info = new WebPartContextualInfo();
                WebPartRibbonContextualGroup contextualGroup = new WebPartRibbonContextualGroup();
                WebPartRibbonTab ribbonTab = new WebPartRibbonTab();

                contextualGroup.Id = "Ribbon.Html5VideoTabGroup";
                contextualGroup.Command = "Html5VideoTab.EnableContextualGroup";
                contextualGroup.VisibilityContext = "Html5VideoTab.CustomVisibilityContext";

                ribbonTab.Id = "Ribbon.Html5VideoRibbon";
                ribbonTab.VisibilityContext = "Html5VideoTab.CustomVisibilityContext";

                info.ContextualGroups.Add(contextualGroup);
                info.Tabs.Add(ribbonTab);
                info.PageComponentId = SPRibbon.GetWebPartPageComponentId(this);

                return info;
            }
        } 
        #endregion

        #region WebPart Properties
        [WebBrowsable(true), Personalizable(true), WebPartStorage(Storage.Shared), WebDisplayName("Video url"), WebDescription(""), SPWebCategoryName("Video options")]
        public string VideoUrl
        {
            get { return _videoUrl; }
            set { _videoUrl = value; }
        } 
        #endregion

        #region Methods
        private void AddContextualTab()
        {
            Ribbon ribbon = SPRibbon.GetCurrent(Page);

            XmlDocument ribbonExtensions = new XmlDocument();
            ribbonExtensions.LoadXml(this.contextualTab);
            ribbon.RegisterDataExtension(ribbonExtensions.FirstChild, "Ribbon.ContextualTabs._children");
            ribbonExtensions.LoadXml(this.contextualTabTemplate);
            ribbon.RegisterDataExtension(ribbonExtensions.FirstChild, "Ribbon.Templates._children");
        } 
        #endregion

        #region Overrides
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            AddContextualTab();

            ClientScriptManager clientScript = Page.ClientScript;
            clientScript.RegisterClientScriptBlock(GetType(), "Html5VideoTabWebPart", DelayScript);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.WriteBeginTag("video");
            writer.WriteAttribute("id", String.Format("{0}_video", ClientID));
            writer.WriteAttribute("src", VideoUrl);
            if (!Width.IsEmpty)
                writer.WriteAttribute("width", (Width.Value - 2).ToString()); //remove 2pixel to avoid web part scroll bars
            if (!Height.IsEmpty)
                writer.WriteAttribute("height", (Height.Value - 2).ToString()); //remove 2pixel to avoid web part scroll bars
            writer.WriteEndTag("video");
        } 
        #endregion
    }
}
